package com.xayy.health.ds.service.impl;

import cn.hutool.core.date.DatePattern;
import com.xayy.health.ds.model.DsTaskParamInfo;
import com.xayy.health.ds.model.DsTelemetryData;
import com.xayy.health.ds.service.DsSelectTelemetryDataService;
import com.xayy.health.ds.service.DsTaskParamInfoService;
import com.xayy.health.mgt.model.dto.TelemetryDto;
import com.xtck.ha.common.core.web.service.dsimpl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.List;


/**
 * @Description: DsTaskParamInfoService实现类
 * @Auther:liuyuan
 * @Date:2025-07-08
 */
@Slf4j
@Service
public class DsDsSelectTelemetryDataServiceImpl extends BaseServiceImpl<DsTaskParamInfo> implements DsSelectTelemetryDataService {


    @Override
    public void createTaskTable(String tableName){
        String sql = "CREATE TABLE IF NOT EXISTS HEALTH." +
                tableName +
                " (\n" +
                "        sate_code \"pg_catalog\".\"varchar\" COLLATE pg_catalog.\"default\",\n" +
                "        param_code \"pg_catalog\".\"varchar\" COLLATE pg_catalog.\"default\",\n" +
                "        value_type \"pg_catalog\".\"varchar\" COLLATE pg_catalog.\"default\",\n" +
                "        value \"pg_catalog\".\"varchar\" COLLATE pg_catalog.\"default\",\n" +
                "        time \"pg_catalog\".\"varchar\" COLLATE pg_catalog.\"default\",\n" +
                "        task_id \"pg_catalog\".\"int8\"\n" +
                "        )";
        selectBySql(sql);

    }

    @Override
    public void insertTelemetryListData(String tableName, List<DsTelemetryData> list){
        String sql = "insert into HEALTH.${tableName} (sate_code, param_code, value_type, time, value,task_id)\n" +
                "        VALUES\n" ;
        for (DsTelemetryData data:list){
            sql += "            (" +
                    "'"+data.getSateCode()+"'" +
                    ", " +
                    "'"+data.getParamCode()+"'" +
                    ", " +
                    "'"+data.getValueType()+"''" +
                    ", CAST(" +
                    "'"+data.getTime()+"'" +
                    " as timestamp),\n" +
                    "  " +
                    "'"+data.getValue()+"'" +
                    "," +
                    "'"+data.getTaskId()+"'" +
                    ")";
        }
        selectBySql(sql);
    }

    @Override
    public void deleteTelemetryData(String tableName){
        String sql = "DELETE from HEALTH." +tableName;
        selectBySql(sql);
    }


    @Override
    public List<DsTelemetryData> selectTelemetryListData(String tableName, TelemetryDto telemetryDto){
        SimpleDateFormat sdf = new SimpleDateFormat(DatePattern.NORM_DATETIME_PATTERN);
        String sql = "SELECT *  FROM  HEALTH." +
                tableName+"\n" +
                "  WHERE\n" +
                "  sate_code = " +
                "'"+telemetryDto.getSatelliteCode()+"'\n" +
                "  AND param_code = " +
                "'"+telemetryDto.getParamCode()+"'\n" +
                "  AND time >= " +
                "'"+sdf.format(telemetryDto.getStartTime())+"'\n"+
                "  AND time  <=  " +
                "'"+sdf.format(telemetryDto.getEndTime())+"'\n"+
                "  ORDER BY\n" +
                "  time ASC";
        return selectBySql(sql,DsTelemetryData.class);
    }

    @Override
    /**
     * 删除表
     * @param tableName 表名
     */
    public void deleteTable(String tableName){
        String sql = "drop table if exists HEALTH."+tableName;
        selectBySql(sql);
    }
}
